package com.semanticweb.project.search.api;

import java.io.IOException;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.Version;

import com.semanticweb.project.search.analyzer.SerbianAnalyzer;

public class Indexer {

    public void index(final Directory indexDir, final AbstractDocument document) throws IOException, IndexingException {

        final IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_34, new SerbianAnalyzer());
        iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        final IndexWriter writer = new IndexWriter(indexDir, iwc);

        final Document doc = document.generateDocument();
        if (doc == null) {
            writer.close();
            throw new IndexingException("Unknown error occured during indexing");
        }

        writer.addDocument(doc);
        writer.close();
    }
}